<?php

class FotoController extends Controller
{
	private function loadModel($id){
		$model=Foto::model()->findByPk($id);
		if($model===null) throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	public function actionDownload($id)
	{
		$model=$this->loadModel($id);
		header('Pragma: public');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Content-Transfer-Encoding: binary');
		header('Content-length: '.$model->ft_filesize);
		header('Content-Type: '.$model->ft_filetype);
		header('Content-Disposition: attachment; filename='.$model->ft_filename);
		echo file_get_contents('uploads/'.$model->ft_filename);
	}
	public function actionDelete($id,$ctrl){
		$model=$this->loadModel($id);
		if(unlink('uploads/'.$model->ft_filename))
		if($model->delete()) $this->redirect(array($this->getReturnUrl($ctrl)));
		throw new CHttpException(400,'Hapus File Gagal.');
	}
	public function actionUpload($ctrl){
		$model=new Foto();
		if(isset($_POST['Foto'])){
			$model->attributes=$_POST['Foto'];
			$model->ft_kode=$ctrl;
			if($model->save()) $this->redirect(array($this->getReturnUrl($ctrl)));
		}
		$this->render('uploadfoto',array('model'=>$model));
	}
	
	public function getReturnUrl($controllercode){
		$returnUrl=array(
			'B1S2'=>'luastanah/lihatFoto',
			'B8S1'=>'luastanamproduksipadisawah/lihatFoto',
		);
		return $returnUrl[$controllercode];
	}
	
}